"use strict";
import * as React from 'react';
const { Text, ViewStyle, Linking } = require("react-native");
import { urlReg } from "../constant";
import { commonStyles } from "../assets/styles/common";
import { openUrl } from "../utils/tools";
class MyText extends React.Component {
    constructor(props, context) {
        super(props, context);
    }
    static onTextPress(url) {
        openUrl(url);
    }
    ;
    static checkLinks(data, result) {
        if (typeof data === 'string') {
            const res = data.split(urlReg);
            const res1 = data.match(urlReg);
            if (res1) {
                let index = 0;
                res.map((item, i) => {
                    if (item.indexOf('http') !== -1 && res1) {
                        result.push(<Text key={i} style={commonStyles.linkText} onPress={MyText.onTextPress.bind(null, res1[index])}>{res1[index]}</Text>);
                        index = index + 1;
                    }
                    else {
                        result.push(item);
                    }
                });
            }
            else {
                result.push(data);
            }
        }
        else if (Array.isArray(data)) {
            data.map(item => {
                MyText.checkLinks(item, result);
            });
        }
        else if (typeof data === 'object') {
            if (data && data['children']) {
                MyText.checkLinks(data.props.children, result);
            }
        }
        return result;
    }
    render() {
        return (<Text {...this.props} style={[{
                color: this.props.color,
                fontSize: this.props.fontSize,
                fontWeight: this.props.fontWeight,
                backgroundColor: 'transparent'
            }, this.props.style]}>{MyText.checkLinks(this.props.children, [])}</Text>);
    }
}
MyText.defaultProps = {
    allowFontScaling: false,
    color: '#333',
    fontSize: 14,
    fontWeight: 'normal'
};
export default MyText;
